#import "ALTimelineEntryElement.h"
#import "ALTimelineElementView.h"
#import "NSObject+BlockObservation.h"


#pragma mark Constants
static const CGFloat ALTimelineEntryLabelPadding = 5.0f;
static const CGFloat ALTimelineEntryBorderPadding = 10.0f;
static const CGFloat ALTimelineLabelFontSize = 11.0f;
static const CGFloat ALTimelineAuxilliaryFontSize = 9.0f;


#pragma mark -
#pragma mark Class Globals
static NSColor *ALTimelineEntryLabelNormalColor;
static NSColor *ALTimelineEntryLabelSelectedColor;
static NSColor *ALTimelineEntryAuxNormalColor;
static NSColor *ALTimelineEntryAuxSelectedColor;


#pragma mark -
@interface ALTimelineEntryElement (ALTimelineEntryElementPrivateMethods)
-(void)updateColorsForSelectionState_:(BOOL)isSelected;
@end


#pragma mark -
@implementation ALTimelineEntryElement

@synthesize labelField;
@synthesize dateField;
@synthesize versionField;
@synthesize valueView;

+(void)initialize {
	ALTimelineEntryLabelNormalColor = [NSColor colorWithCalibratedWhite:0.502 alpha:1.000];
	ALTimelineEntryLabelSelectedColor = [NSColor colorWithCalibratedWhite:0.094 alpha:1.000];
	ALTimelineEntryAuxNormalColor = [NSColor colorWithCalibratedWhite:0.400 alpha:1.000];
	ALTimelineEntryAuxSelectedColor = [NSColor colorWithCalibratedWhite:0.169 alpha:1.000];
}

-(void)timelineElementWasAddedToTimeline {
	[self updateColorsForSelectionState_:NO];
	AMBlockTask selectedTask = ^(id object, NSDictionary *change) {
		[self updateColorsForSelectionState_:[self selected]];
	};
	[self addObserverForKeyPath:ALSelectedKey task:selectedTask];
	
	[self tile];
	AMBlockTask layoutTask = ^(id object, NSDictionary *change) {
		[self tile];
		ALTimelineCanvas *canvas = [[self timeline] canvas];
		[canvas layout];
	};
	[[self representedObject] addObserverForKeyPath:ALLabelKey task:layoutTask];
}

-(NSSize)preferredSize {
	NSSize labelSize = [[[self labelField] cell] cellSize];
	NSSize valueSize = [[[self valueView] cell] cellSize];
	NSRect valueFrame = [[self valueView] frame];
	CGFloat width = round(labelSize.width + valueSize.width + ALTimelineEntryLabelPadding + (2 * ALTimelineEntryBorderPadding));
	CGFloat height = round(valueFrame.origin.y + valueSize.height + ALTimelineEntryBorderPadding);
	return NSMakeSize(width, height);
}

-(NSSize)minimumSize {
	return [self preferredSize];
}

-(NSSize)maximumSize {
//	NSSize preferred = [self preferredSize];
//	preferred.width = CGFLOAT_MAX;
//	return preferred;
	return [self preferredSize];
}

-(void)tile {
	NSRect viewFrame = [[self view] frame];
	NSRect valueFrame = [[self valueView] frame];
	NSRect labelFrame = [[self labelField] frame];
	NSSize minCellSize = [[[self labelField] cell] cellSize];
	labelFrame.size.width = minCellSize.width;
	labelFrame = NSIntegralRect(labelFrame);
	[[self labelField] setFrame:labelFrame];
	
	valueFrame.origin.x = NSMaxX(labelFrame) + ALTimelineEntryLabelPadding;
	valueFrame.size.width = viewFrame.size.width - ALTimelineEntryBorderPadding - valueFrame.origin.x;
	[[self valueView] setFrame:valueFrame];
}

-(void)updateColorsForSelectionState_:(BOOL)isSelected {
	[(ALTimelineElementView *)[self view] setSelected:isSelected];
	NSColor *labelColor = isSelected ? ALTimelineEntryLabelSelectedColor : ALTimelineEntryLabelNormalColor;
	NSColor *auxColor = isSelected ? ALTimelineEntryAuxSelectedColor : ALTimelineEntryAuxNormalColor;
	[[self labelField] setTextColor:labelColor];
	[[self dateField] setTextColor:auxColor];
	[[self versionField] setTextColor:auxColor];
}

@end
